/**
 * @Author wangyl
 * @E-mail wangyl0629@foxmail.com
 **/
package com.wyl.xue.admin.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyl.xue.admin.system.mybatis.entity.SystemDepartment;
import com.wyl.xue.admin.system.mybatis.service.SystemDepartmentService;
import com.wyl.xue.admin.system.mybatis.service.SystemUsersService;
import com.wyl.xue.admin.system.service.Department;
import com.wyl.xue.admin.system.vo.DepartmentInfoVo;
import com.wyl.xue.admin.system.vo.DepartmentTree;
import com.wyl.xue.core.util.UserTools;
import com.wyl.xue.core.util.exception.BizException;
import com.wyl.xue.core.util.result.ResultCode;
import com.wyl.xue.core.util.tree.TreeUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.sf.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName: DepartmentImpl
 * @Function: TODO
 * @Date: 2020/11/7 22:37
 * @author wyl
 * @version V1.0
 */
@Service
@Slf4j
@AllArgsConstructor
public class DepartmentImpl implements Department {

    /**
     * 复制对象 从DepartmentInfoVo 复制为SystemDepartment
     */
    final BeanCopier copier = BeanCopier.create(DepartmentInfoVo.class, SystemDepartment.class, false);
    final SystemDepartmentService systemDepartmentService;
    final SystemUsersService systemUsersService;

    /**
     * @Description 添加部门信息
     * @param departmentInfoVo
     * @return void
     * @Date 2020/11/8 17:20
     * @Author wangyl
     * @Version V1.0
     */
    @Override
    public void addDepartment(DepartmentInfoVo departmentInfoVo) {
        SystemDepartment systemDepartment = new SystemDepartment();
        copier.copy(departmentInfoVo, systemDepartment, null);
        systemDepartment.setOperator(UserTools.tools.getUserNameByRequest());
        systemDepartmentService.save(systemDepartment);
    }

    /**
     * @Description 修改部门信息
     * @param systemDepartment
     * @return void
     * @Date 2020/11/8 17:21
     * @Author wangyl
     * @Version V1.0
     */
    @Override
    public void updateDepartment(SystemDepartment systemDepartment) {
        systemDepartment.setOperator(UserTools.tools.getUserNameByRequest());
        systemDepartmentService.updateById(systemDepartment);
    }

    /**
     * @Description 通过id删除部门信息
     * @param id
     * @return void
     * @Date 2020/11/8 17:22
     * @Author wangyl
     * @Version V1.0
     */
    @Override
    public void deleteDepartment(long id) {
        if (systemUsersService.getSystemUsersByDepartmentId(id)
                              .isEmpty() && !systemDepartmentService.hasChildDepartment(id)) {
            systemDepartmentService.removeById(id);
        } else {
            throw new BizException(ResultCode.HAVERESOURCES);
        }
    }

    /**
     * @Description 通过父Id获取所有部门树
     * @param parentId
     * @return java.util.List<com.wyl.xue.admin.system.vo.DepartmentTree>
     * @Date 2020/11/8 17:25
     * @Author wangyl
     * @Version V1.0
     */
    @Override
    public List<DepartmentTree> getDepartmentTree(long parentId) {
        List<SystemDepartment> systemDepartmentList = systemDepartmentService.list();
        List<DepartmentTree> departmentTrees = systemDepartmentList.parallelStream()
                                                                   .map(systemDepartment -> new DepartmentTree(systemDepartment.getDepartmentId(), systemDepartment.getParentId(), systemDepartment.getDepartmentName()))
                                                                   .collect(Collectors.toList());
        return TreeUtil.bulid(departmentTrees, parentId);
    }

    /**
     * @Description 通过父级id分页获取子部门
     * @param parentId
     * @param page
     * @param size
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.wyl.xue.admin.system.mybatis.entity.SystemDepartment>
     * @Date 2020/11/8 17:27
     * @Author wangyl
     * @Version V1.0
     */
    @Override
    public IPage<SystemDepartment> getSubdirectoryDepartmentByParentIdForPage(long parentId, int page, int size) {
        Page<SystemDepartment> pageInfo = new Page<>(page, size);
        IPage<SystemDepartment> systemDepartmentPage = systemDepartmentService.page(pageInfo, Wrappers.<SystemDepartment>lambdaQuery().eq(SystemDepartment::getParentId, parentId));
        return systemDepartmentPage;
    }

    /**
     * @Description 获取部门下的所有子部门id
     * @param parentId
     * @return java.util.List<java.lang.Long>
     * @Date 2020/11/13 17:34
     * @Author wangyl
     * @Version V1.0
     */
    @Override
    public List<Object> getDepartmentAllChildNodes(long parentId) {
        List<Object> res = new ArrayList<>();
        List<DepartmentTree> departmentTree = this.getDepartmentTree(parentId);
        TreeUtil.getTreeChildrenIds(departmentTree, res);
        return res;
    }

    /**
     * @Description 搜索部门
     * @param departmentInfoVo
     * @return java.util.List<com.wyl.xue.admin.system.mybatis.entity.SystemDepartment>
     * @Date 2020/11/21 22:23
     * @Author wangyl
     * @Version V1.0
     */
    @Override
    public List<SystemDepartment> searchDepartment(DepartmentInfoVo departmentInfoVo) {
        LambdaQueryWrapper<SystemDepartment> departmentLambdaQueryWrapper = Wrappers.lambdaQuery(SystemDepartment.class)
                                                                                    .like(!Objects.isNull(departmentInfoVo.getDepartmentName()), SystemDepartment::getDepartmentName, departmentInfoVo.getDepartmentName());
        return systemDepartmentService.list(departmentLambdaQueryWrapper);
    }
}
